//
//  ListDetailViewController.swift
//  TimMusic
//
//  Created by nkl on 2024/10/11.
//

import Foundation
import KLSwiftyKit
import TSVideoKit
import UIKit

enum ListDetailType {
    case favourite
    case recently
    case mostPlayed
}

class ListDetailViewController: BasicBGViewController {
    lazy var backButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "ic_back"), for: .normal)
        btn.addTarget(self, action: #selector(dimissCurrentVc), for: .touchUpInside)
        return btn
    }()

    lazy var titleView: UILabel = {
        let label: UILabel = .simpleLabel(text: "Favorite", font: .PoppinsFont(size: 32), color: .white)
        switch listType {
        case .favourite:
            label.text = "Favorite"
        case .recently:
            label.text = "Recently"
        case .mostPlayed:
            label.text = "Most Played"
        }
        return label
    }()

    lazy var countLabel: UILabel = .simpleLabel(text: "0", font: .boldSystemFont24, color: .white)
    lazy var bgView: UIView = .simpleView(color: UIColor.white.withAlphaComponent(0.1))

    lazy var listVc: UIViewController = {
        switch listType {
        case .favourite:
            var vModel = FavouriteViewModel()
            let vc = SongListViewController(viewModel: vModel)
            vc.emptyView.importButton.isHidden = true
            return vc
        case .recently:
            var vModel = RecentlyViewModel()
            let vc = SongListViewController(viewModel: vModel)
            vc.emptyView.importButton.isHidden = true
            return vc
        case .mostPlayed:
            var vModel = MostPlayedViewModel()
            let vc = SongListViewController(viewModel: vModel)
            vc.emptyView.importButton.isHidden = true
            return vc
        }

    }()

    lazy var playAllBtnBg: UIImageView = .simpleImage(imageName: "ic_detail_play_all")
    lazy var playAll: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "ic_detail_play"), for: .normal)
        btn.addTarget(self, action: #selector(playAllVideo), for: .touchUpInside)
        return btn
    }()

    var listType: ListDetailType

    init(listType: ListDetailType) {
        self.listType = listType
        super.init(nibName: nil, bundle: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        addNotifaction()
    }

    func addNotifaction() {
        NotificationCenter.default.addObserver(self, selector: #selector(reloadInfoData), name: kDataChangedNotifactionName, object: nil)
    }

    @objc func reloadInfoData() {
        updateCount()
    }

    @objc func playAllVideo() {
        if let vc = listVc as? SongListViewController<FavouriteViewModel>,
           let first = vc.viewModel.datas.first {
            PlayDetailManager.shared.playVideo(video: first, list: vc.viewModel.datas, scene: .local)
        }
        if let vc = listVc as? SongListViewController<RecentlyViewModel>,
           let first = vc.viewModel.datas.first {
            PlayDetailManager.shared.playVideo(video: first, list: vc.viewModel.datas, scene: .local)
        }
        if let vc = listVc as? SongListViewController<MostPlayedViewModel>,
           let first = vc.viewModel.datas.first {
            PlayDetailManager.shared.playVideo(video: first, list: vc.viewModel.datas, scene: .local)
        }
    }

    override func addChildren() {
        super.addChildren()
        view.addSubview(backButton)
        view.addSubview(titleView)
        view.addSubview(countLabel)
        view.addSubview(bgView)
        bgView.addSubview(listVc.view)
        playAllBtnBg.isUserInteractionEnabled = false
        view.addSubview(playAllBtnBg)
        playAllBtnBg.isUserInteractionEnabled = true
        playAllBtnBg.addSubview(playAll)
        listVc.view.backgroundColor = .clear
        addChild(listVc)
        updateCount()
    }

    func updateCount() {
        if let vc = listVc as? SongListViewController<FavouriteViewModel> {
            countLabel.text = "\(vc.viewModel.datas.count)"
        }
        if let vc = listVc as? SongListViewController<RecentlyViewModel> {
            countLabel.text = "\(vc.viewModel.datas.count)"
        }
        if let vc = listVc as? SongListViewController<MostPlayedViewModel> {
            countLabel.text = "\(vc.viewModel.datas.count)"
        }

        TMTabbarViewController.rootTabbar?.hideSelectedBar()
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        TMTabbarViewController.rootTabbar?.showSelectedBar()
    }

    @MainActor required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @objc func dimissCurrentVc() {
        navigationController?.popViewController(animated: true)
    }

    override func makeConstraints() {
        super.makeConstraints()
        backButton.snp.makeConstraints { make in
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.width.height.equalTo(24)
            make.leading.equalToSuperview().offset(16)
        }

        titleView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.top.equalTo(backButton.snp.bottom).offset(20)
        }

        countLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.top.equalTo(titleView.snp.bottom).offset(16)
        }

        playAllBtnBg.snp.makeConstraints { make in
            make.bottom.equalTo(listVc.view.snp.top)
            make.trailing.equalToSuperview().offset(-40)
        }

        playAll.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }

        bgView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalTo(countLabel.snp.bottom).offset(20)
            make.bottom.equalToSuperview()
        }

        listVc.view.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.top.equalTo(countLabel.snp.bottom).offset(20)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(-tabbarHeight)
        }
    }
}
